package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportSmsSendLogRpcService;
import com.hunttown.mes.rpc.domain.TransportSmsSendLogDTO;
import com.hunttown.mes.rpc.domain.query.TransportSmsSendLogDTOQuery;
import com.hunttown.mes.service.TransportSmsSendLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportSmsSendLogConverts.convert;

/**
 * created by wangjunfu on 2019-05-19 01:20:15
 */
@Service
public class TransportSmsSendLogRpcServiceImpl implements TransportSmsSendLogRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportSmsSendLogRpcServiceImpl.class);

    private final TransportSmsSendLogService transportSmsSendLogService;

    @Autowired
    public TransportSmsSendLogRpcServiceImpl(TransportSmsSendLogService transportSmsSendLogService) {
        this.transportSmsSendLogService = transportSmsSendLogService;
    }

    @Override
    public TransportSmsSendLogDTO insert(TransportSmsSendLogDTO transportSmsSendLogDTO) {
        try {
            return convert(transportSmsSendLogService.insert(convert(transportSmsSendLogDTO)));
        } catch (Exception e) {
            logger.error("TransportSmsSendLog create failed.", e);
            throw new RuntimeException("TransportSmsSendLog create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportSmsSendLogDTO transportSmsSendLogDTO) {
        try {
            return transportSmsSendLogService.updateInfoById(convert(transportSmsSendLogDTO));
        } catch (Exception e) {
            logger.error("TransportSmsSendLog update by id failed.", e);
            throw new RuntimeException("TransportSmsSendLog update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportSmsSendLogDTOQuery transportSmsSendLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportSmsSendLogDTOQuery.getW_id() != null && transportSmsSendLogDTOQuery.getW_id() > 0) {
                query.put("w_id", transportSmsSendLogDTOQuery.getW_id());
            }

            if (transportSmsSendLogDTOQuery.getEntId() != null && transportSmsSendLogDTOQuery.getEntId() > 0) {
                query.put("entId", transportSmsSendLogDTOQuery.getEntId());
            }

            if (transportSmsSendLogDTOQuery.getStatus() != null) {
                query.put("status", transportSmsSendLogDTOQuery.getStatus());
            }

            if (transportSmsSendLogDTOQuery.getIds() != null && !transportSmsSendLogDTOQuery.getIds().equals("")) {
                query.put("ids", transportSmsSendLogDTOQuery.getIds());
            }

            return transportSmsSendLogService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportSmsSendLog update by query failed.", e);
            throw new RuntimeException("TransportSmsSendLog update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportSmsSendLogDTO selectById(Integer id) {
        try {
            return convert(transportSmsSendLogService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportSmsSendLog select by id failed.", e);
            throw new RuntimeException("TransportSmsSendLog select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportSmsSendLogDTO selectObjByQuery(TransportSmsSendLogDTOQuery transportSmsSendLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportSmsSendLogDTOQuery.getEntId() != null && transportSmsSendLogDTOQuery.getEntId() > 0) {
                query.put("entId", transportSmsSendLogDTOQuery.getEntId());
            }

            if (StringUtils.isNotBlank(transportSmsSendLogDTOQuery.getMobile())) {
                query.put("mobile", transportSmsSendLogDTOQuery.getMobile());
            }

            if (StringUtils.isNotBlank(transportSmsSendLogDTOQuery.getOrderBy())) {
                query.put("orderBy", transportSmsSendLogDTOQuery.getOrderBy());
            }

            return convert(transportSmsSendLogService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportSmsSendLog select obj by query failed.", e);
            throw new RuntimeException("TransportSmsSendLog select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportSmsSendLogDTO> getListForPage(TransportSmsSendLogDTOQuery transportSmsSendLogDTOQuery) {
        Query query = Query.page(transportSmsSendLogDTOQuery.getPn(), transportSmsSendLogDTOQuery.getPageSize());

        if (transportSmsSendLogDTOQuery.getId() != null && transportSmsSendLogDTOQuery.getId() > 0) {
            query.put("id", transportSmsSendLogDTOQuery.getId());
        }

        if (transportSmsSendLogDTOQuery.getIds() != null && !transportSmsSendLogDTOQuery.getIds().equals("")) {
            query.put("ids", transportSmsSendLogDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportSmsSendLogDTOQuery.getLikeName())) {
            query.put("likeName", transportSmsSendLogDTOQuery.getLikeName());
        }

        if (transportSmsSendLogDTOQuery.getEntId() != null && transportSmsSendLogDTOQuery.getEntId() > 0) {
            query.put("entId", transportSmsSendLogDTOQuery.getEntId());
        }

        if (transportSmsSendLogDTOQuery.getInfoType() != null) {
            query.put("infoType", transportSmsSendLogDTOQuery.getInfoType());
        }

        if (transportSmsSendLogDTOQuery.getUserType() != null) {
            query.put("userType", transportSmsSendLogDTOQuery.getUserType());
        }

        if (transportSmsSendLogDTOQuery.getSmsType() != null) {
            query.put("smsType", transportSmsSendLogDTOQuery.getSmsType());
        }

        if (StringUtils.isNotBlank(transportSmsSendLogDTOQuery.getMobile())) {
            query.put("mobile", transportSmsSendLogDTOQuery.getMobile());
        }

        if (StringUtils.isNotBlank(transportSmsSendLogDTOQuery.getShowDate())) {
            query.put("showDate", transportSmsSendLogDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportSmsSendLogDTOQuery.getStartDate())) {
            query.put("startDate", transportSmsSendLogDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportSmsSendLogDTOQuery.getEndDate())) {
            query.put("endDate", transportSmsSendLogDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportSmsSendLogDTOQuery.getOrderBy())) {
            query.put("orderBy", transportSmsSendLogDTOQuery.getOrderBy());
        }

        //区间日期
        if (StringUtils.isNotBlank(transportSmsSendLogDTOQuery.getDaterange())) {
            String[] date = transportSmsSendLogDTOQuery.getDaterange().split("~");
            if (date.length == 2) {
                query.put("startDate", date[0].trim());
                query.put("endDate", date[1].trim());
            }
        }

        List<TransportSmsSendLogDTO> list = convert(transportSmsSendLogService.selectByQuery(query));
        Page<TransportSmsSendLogDTO> page = Page.from(transportSmsSendLogDTOQuery.getPn(), transportSmsSendLogDTOQuery.getPageSize(), list);
        page.setTotalItems(transportSmsSendLogService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportSmsSendLogService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportSmsSendLog delete failed.", e);
            throw new RuntimeException("TransportSmsSendLog delete failed, Please order your code.", e);
        }
    }
}